/**
 * Bakalarska prace - Inteligentni budik
 * @package sleepCatcher
 * @file FormAlarmAwakeSound.cpp
 * @author Jan Pesava - xpesav00
 * @email xpesav00@stud.fit.vutbr.cz
 * @date 24. 12. 2011
 */

#include <FIoDirectory.h>
#include <FGrpColor.h>
#include <FGrpDimension.h>
#include <FGrpRectangle.h>
#include "FMedia.h"

#include "forms/FormAlarmAwakeSound.h"
#include "SCFormManager.h"
#include "SCTranslator.h"

using namespace Osp::Io;
using namespace Osp::Graphics;
using namespace Osp::Media;




FormAlarmAwakeSound::FormAlarmAwakeSound()
{
	this->pAlarm = NULL;
}

FormAlarmAwakeSound::~FormAlarmAwakeSound()
{
	this->pAlarmSounds->RemoveAll(true);
	delete this->pAlarmSounds;
}


bool
FormAlarmAwakeSound::Initialize()
{
	Construct(L"IDF_ALARM_AWAKE_SOUND");
	return true;
}

result
FormAlarmAwakeSound::OnInitializing(void)
{
	result r = E_SUCCESS;

	// prepare arraylist
	this->pAlarmSounds = new ArrayList();

	// Set Sounds Name
	this->PrepareAlarmSound();

	// Set Graphics
	this->pListViewSounds = static_cast<ListView*>(GetControl("IDC_LISTVIEW_SOUNDS"));
	this->pListViewSounds->SetItemProvider(*this);
	this->pListViewSounds->AddListViewItemEventListener(*this);
	this->pListViewSounds->Draw();


	Footer* pFooter = GetFooter();
	Header* pHeader = GetHeader();

	HeaderItem* pAlarmSound = new HeaderItem();
	pAlarmSound->Construct(IDA_ALARM_SOUND);
	pAlarmSound->SetText(SCTranslator::GetText("IDS_ALARMSSOUND_ALARMTONE"));

	HeaderItem* pMySounds = new HeaderItem();
	pMySounds->Construct(IDA_MY_SOUNDS);
	pMySounds->SetText(SCTranslator::GetText("IDS_ALARMSSOUND_MYSOUNDS"));

	FooterItem* pBack = new FooterItem();
	pBack->Construct(IDA_SOUND_BACK);
	pBack->SetText(SCTranslator::GetText("IDS_ALARMS_BACK"));

	pHeader->SetStyle(HEADER_STYLE_SEGMENTED);
	pHeader->AddItem(*pAlarmSound);
	pHeader->AddItem(*pMySounds);
	pHeader->AddActionEventListener(*this);

	pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
	pFooter->AddItem(*pBack);
	pFooter->AddActionEventListener(*this);

	delete pBack;
	delete pMySounds;
	delete pAlarmSound;

	return r;
}

result
FormAlarmAwakeSound::OnTerminating(void)
{
	result r = E_SUCCESS;
	return r;
}

void
FormAlarmAwakeSound::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
{
	ArrayList* pParams;

	switch(actionId)
	{
		case IDA_SOUND_BACK:
			pParams = new ArrayList();
			pParams->Add(*this->pAlarm);
			SCFormManager::SwitchToForm(SCFormManager::IDF_ALARM_SET, false, pParams, SCFormManager::SLIDE_RIGHT);
			pParams->RemoveAll();
			delete pParams;
			break;
	}
}

void
FormAlarmAwakeSound::OnListViewContextItemStateChanged (Osp::Ui::Controls::ListView &listView, int index, int elementId, Osp::Ui::Controls::ListContextItemStatus status)
{
}

void
FormAlarmAwakeSound::OnListViewItemLongPressed (Osp::Ui::Controls::ListView &listView, int index, int elementId, bool &invokeListViewItemCallback)
{
}

void
FormAlarmAwakeSound::OnListViewItemStateChanged (Osp::Ui::Controls::ListView &listView, int index, int elementId, Osp::Ui::Controls::ListItemStatus status)
{
	ArrayList* pParams = new ArrayList();
	pParams->Add(*this->pAlarm);

	if(this->pAlarm != NULL)
	{
		pAlarm->SetAwakeSound(*(static_cast<String*>(this->pAlarmSounds->GetAt(index))));
	}

	SCFormManager::SwitchToForm(SCFormManager::IDF_ALARM_SET, false, pParams, SCFormManager::SLIDE_RIGHT);

	pParams->RemoveAll();
	delete pParams;
}

void
FormAlarmAwakeSound::OnListViewItemSwept (Osp::Ui::Controls::ListView &listView, int index, Osp::Ui::Controls::SweepDirection direction)
{
}

Osp::Ui::Controls::ListItemBase*
FormAlarmAwakeSound::CreateItem(int index, int itemWidth)
{
	CustomItem* pCustomItem = NULL;
	int nameSize = 40;
	Color* pNameColor;
	Bitmap* pBitmap;
	String* alarmSoundName;
	String notePath("/Home/Icons/Commons/note.png");

	if(this->pAlarmSounds != NULL)
	{
		pNameColor = new Color(Color::COLOR_WHITE);
		Image *pImage = new Image();
		pImage->Construct();

		pBitmap = pImage->DecodeN(notePath, BITMAP_PIXEL_FORMAT_RGB565, 100, 100);

		if(pBitmap == NULL)
			AppLog("Image not loaded.");

		alarmSoundName = static_cast<String*>(this->pAlarmSounds->GetAt(index));

		pCustomItem = new CustomItem();
		pCustomItem->Construct(Dimension(itemWidth,100), LIST_ANNEX_STYLE_RADIO);
		pCustomItem->AddElement(Rectangle(10,10,80,80), LIST_ITEM_PLAY, *pBitmap);
		pCustomItem->AddElement(Rectangle(100,0,itemWidth-150,100), LIST_ITEM_NAME, *alarmSoundName, nameSize, *pNameColor, *pNameColor, *pNameColor, true);

		delete pImage;
		delete pNameColor;
	}

	return pCustomItem;
}

bool
FormAlarmAwakeSound::DeleteItem(int index, Osp::Ui::Controls::ListItemBase* pItem, int itemWidth)
{
	delete pItem;
	pItem = NULL;
	return true;
}

int
FormAlarmAwakeSound::GetItemCount(void)
{
	return this->pAlarmSounds->GetCount();
}


int
FormAlarmAwakeSound::GetFormId()
{
	return SCFormManager::IDF_ALARM_AWAKE_SOUND;
}

void
FormAlarmAwakeSound::PrepareAlarmSound()
{
	Directory* pDir = NULL;
	DirEnumerator* pDirEnum = NULL;
	String alarmSoundPath(L"/Home/alarm_sounds/");
	String* fileName = NULL;

	pDir = new Directory();
	if(IsFailed(pDir->Construct(alarmSoundPath)))
	{
		AppLog("Directory not found.");
	}

	pDirEnum = pDir->ReadN();

	if(pDirEnum != NULL)
	{
		while(pDirEnum->MoveNext() == E_SUCCESS)
		{
			DirEntry entry = pDirEnum->GetCurrentDirEntry();
			fileName = new String(entry.GetName());
			this->pAlarmSounds->Add(*fileName);
		}

		delete pDirEnum;
	}

	delete pDir;
}

void
FormAlarmAwakeSound::SetAlarm(SCAlarm* pAlarm)
{
	this->pAlarm = pAlarm;
}

SCAlarm*
FormAlarmAwakeSound::GetAlarm()
{
	return this->pAlarm;
}

void
FormAlarmAwakeSound::Update()
{
	String *alarmSoundName;

	// Set item checkted
	for(int i=0; i < this->pAlarmSounds->GetCount(); i++)
	{
		alarmSoundName = static_cast<String*>(this->pAlarmSounds->GetAt(i));
		if(*alarmSoundName == this->pAlarm->GetAwakeSound())
		{
			this->pListViewSounds->SetItemChecked(i, true);
		} else {
			this->pListViewSounds->SetItemChecked(i, false);
		}
	}
}

void
FormAlarmAwakeSound::Redraw()
{
	this->Update();
	RequestRedraw(true);
}
